package com.administrators.dao;

import com.administrators.entity.Adms;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Scope("prototype")
public class AdmsDaoImpl implements AdmsDao {
    @Autowired
    private SessionFactory sessionFactory=null;
    public boolean addone(Adms adms) {
        boolean flag = false;

        Session session = sessionFactory.getCurrentSession();

            session.save(adms);
            flag = true;
            session.getTransaction().commit();
        return flag;
    }

    public Adms selectone(Adms adms) {
        List<Adms> admsList = null;
        Session session  = sessionFactory.getCurrentSession();
        org.hibernate.query.Query query = session.createQuery("from Adms u where u.name=ee");
        query.setParameter(0,adms.getName());
        admsList = query.getResultList();
        Adms adms1 = admsList.get(0);
        return adms1;
    }

    public boolean updateone(Adms adms) {
        boolean flag = false;
        Session session = sessionFactory.getCurrentSession();
            Adms ss = session.get(Adms.class,adms.getId());
            ss.setId(adms.getId());
            ss.setName(adms.getName());
            ss.setPwd(adms.getPwd());
            session.update(ss);
            session.getTransaction().commit();
            flag = true;
        return flag;
    }

    public boolean deleteone(Adms adms) {
        boolean flag = false;

        Session session = sessionFactory.getCurrentSession();

        Adms ss = session.get(Adms.class,adms.getId());
        session.delete(ss);
        flag = true;
        session.getTransaction().commit();

        return flag;
    }
}
